<!DOCTYPE html>
<html lang="zh-Hans">

<!-- Head tag -->
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="Note">
    

    <!--Author-->
    
        <meta name="author" content="兰陵忧患生">
    

    <!-- Title -->
    
    <title>第二部分 Web 中的 Spring | Lan</title>

    <!-- Bootstrap Core CSS -->
    <link href="//cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet"
      href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.12.0/build/styles/default.min.css">
    <script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.12.0/build/highlight.min.js"></script>

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/css/style.css">

    <!-- Custom Fonts -->
    <link href="//cdn.bootcss.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href='//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

    <!-- Content -->
    <section class="article-container">
<!-- Back Home -->
<a class="nav-back" href="/">
    <i class="fa fa-puzzle-piece"></i>
</a>

<!-- Page Header -->
<header class="intro-header">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>第二部分 Web 中的 Spring</h1>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">
            <!-- TOC -->
            <div class="toc-article col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#构建-Spring-Web-应用程序"><span class="toc-text">构建 Spring Web 应用程序</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Spring-MVC-起步"><span class="toc-text">Spring MVC 起步</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#跟踪-Spring-MVC-的请求"><span class="toc-text">跟踪 Spring MVC 的请求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#搭建-Spring-MVC"><span class="toc-text">搭建 Spring MVC</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#配置-DispatcherServlet"><span class="toc-text">配置 DispatcherServlet</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#启用-Spring-MVC"><span class="toc-text">启用 Spring MVC</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#编写基本的控制器"><span class="toc-text">编写基本的控制器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#接受请求的输入"><span class="toc-text">接受请求的输入</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#处理查询参数"><span class="toc-text">处理查询参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#通过路径参数接收输入"><span class="toc-text">通过路径参数接收输入</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#处理表单"><span class="toc-text">处理表单</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#渲染Web视图"><span class="toc-text">渲染Web视图</span></a></li></ol>
</div>

            <!-- Post Main Content -->
            <div class="post-content col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h1 id="构建-Spring-Web-应用程序"><a href="#构建-Spring-Web-应用程序" class="headerlink" title="构建 Spring Web 应用程序"></a>构建 Spring Web 应用程序</h1><h2 id="Spring-MVC-起步"><a href="#Spring-MVC-起步" class="headerlink" title="Spring MVC 起步"></a>Spring MVC 起步</h2><h3 id="跟踪-Spring-MVC-的请求"><a href="#跟踪-Spring-MVC-的请求" class="headerlink" title="跟踪 Spring MVC 的请求"></a>跟踪 Spring MVC 的请求</h3><h3 id="搭建-Spring-MVC"><a href="#搭建-Spring-MVC" class="headerlink" title="搭建 Spring MVC"></a>搭建 Spring MVC</h3><h4 id="配置-DispatcherServlet"><a href="#配置-DispatcherServlet" class="headerlink" title="配置 DispatcherServlet"></a>配置 DispatcherServlet</h4><ul>
<li>通过 AbstractAnnotationConfigDispatcherServletInitializer 配置 DispatcherServlet 需要Servlet3.0，如Tomcat7或更高版本 (替代了web.xml)</li>
</ul>
<h4 id="启用-Spring-MVC"><a href="#启用-Spring-MVC" class="headerlink" title="启用 Spring MVC"></a>启用 Spring MVC</h4><ul>
<li><p>配置类</p>
<ul>
<li>配置视图解析器 InternalResourceViewResolver ( 在视图名称上添加前缀后缀)</li>
<li>启用组件扫描</li>
</ul>
</li>
</ul>
<h2 id="编写基本的控制器"><a href="#编写基本的控制器" class="headerlink" title="编写基本的控制器"></a>编写基本的控制器</h2><ul>
<li><code>@Controller</code></li>
<li><code>@RequestMapping(value=&quot;/&quot;, method=GET)</code></li>
<li>处理器方法上的<code>@RequestMapping</code>注解是对类级别上的<code>@RequestMapping</code>的声明进行补充</li>
<li><code>@ResquestMapping(&quot;/&quot;)</code></li>
<li><code>@ResquestMapping({&quot;/&quot;, &quot;/homepage&quot;})</code></li>
</ul>
<h2 id="接受请求的输入"><a href="#接受请求的输入" class="headerlink" title="接受请求的输入"></a>接受请求的输入</h2><ul>
<li><p>SpringMVC允许以多种方式将客户端中的数据传送到控制器的处理器方法中</p>
<ul>
<li>查询参数(Query Parameter)</li>
<li>表单参数(Form Paramter)</li>
<li>路径参数(Path Variable)</li>
</ul>
</li>
</ul>
<h3 id="处理查询参数"><a href="#处理查询参数" class="headerlink" title="处理查询参数"></a>处理查询参数</h3><pre><code class="Java">    @RequestMapping(method = RequestMethod.GET)
    public List&lt;String&gt; spittles(
            @RequestParam(value = &quot;max&quot;, defaultValue = MAX_LONG_AS_STRING) long max,
            @RequestParam(value = &quot;count&quot;, defaultValue = &quot;20&quot;) int count
    ) {
        return spittleRepository.findSpittles(Long.MAX_VALUE, 20);
    }
</code></pre>
<h3 id="通过路径参数接收输入"><a href="#通过路径参数接收输入" class="headerlink" title="通过路径参数接收输入"></a>通过路径参数接收输入</h3><pre><code class="Java">    @RequestMapping(value = &quot;/{spittleId}&quot;, method = RequestMethod.GET)
    public String spittle(
            @PathVariable(&quot;spittleId&quot;) long spittleId, Model model
    ) {
        model.addAttribute(spittleRepository, findOne(spittleId));
        return &quot;spittle&quot;;
    }

    // 因为方法的参数名与占位符的名称相同，可以去掉@PathVariable中的value属性
    @RequestMapping(value = &quot;/{spittleId}&quot;, method = RequestMethod.GET)
    public String spittle(
            @PathVariable long spittleId, Model model
    ) {
        model.addAttribute(spittleRepository, findOne(spittleId));
        return &quot;spittle&quot;;
    }
</code></pre>
<h2 id="处理表单"><a href="#处理表单" class="headerlink" title="处理表单"></a>处理表单</h2><ul>
<li><p>当InternalResourceViewResolver看到的视图格式是：</p>
<ul>
<li>“Redirect:/spitter/“ + spitter.getUsername();</li>
<li>“forward:/…”</li>
</ul>
</li>
<li><p>校验表单 </p>
<ul>
<li><p>注解 ( javax.validation.constraints )</p>
<ul>
<li>@NotNull</li>
<li>@Size(min=5, max=25)</li>
</ul>
</li>
<li><p>过滤</p>
<pre><code class="Java">  public String processRegistration(
      @Valid Spitter spitter, Errors errors
  ) {
      if(errors.hasErrors()) {
          return ;
      }
  }
</code></pre>
</li>
</ul>
</li>
</ul>
<h1 id="渲染Web视图"><a href="#渲染Web视图" class="headerlink" title="渲染Web视图"></a>渲染Web视图</h1>
 
                <!-- Meta -->
                <div class="post-meta">
                    <hr>
                    <br>
                    <div class="post-tags">
                        
                            

<a href="/tags/Spring-in-Action/">#Spring in Action</a>


                        
                    </div>
                    <div class="post-date">
                        2017 年 04 月 08 日
                    </div>
                </div>
            </div>

            <!-- Comments -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <!-- Disqus Comments -->


            </div>
        </div>
    </div>
</article>
</section>

    <!-- Scripts -->
    <!-- jQuery -->
<script src="//cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="//cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script type="text/javascript">
	hljs.initHighlightingOnLoad();
	console.log('Hexo-theme-hollow designed by zchen9 http://www.chen9.info © 2015-' + (new Date()).getFullYear());
</script>

    <!-- Google Analytics -->
    

</body>

</html>